<?php

declare (strict_types=1);

namespace app\middleware;

use tauthz\facade\Enforcer;

class LoadAuthPack
{

    /**
     * 处理请求
     *
     * @param \think\Request $request
     * @param \Closure $next
     * @return Response
     */
    public function handle($request, \Closure $next)
    {
        $pathInfo = auth()->getPathInfo($request->pathinfo());
        $method = $request->method();
        if (!empty($pathInfo)) {
            // 公用资源
            if (Enforcer::enforce(auth()::COMMEN, $pathInfo, $method)) {
                return $next($request);
            }
        } else {
            return $next($request);
        }
        // 是否登录
        if (!auth()->isLogin()) {
            return redirect('/');
        }
        if (Enforcer::enforce(auth()::PROTECT, $pathInfo, $method)) {
            return $next($request);
        }
        // 分配权限校验
//        if (true != Enforcer::enforce(auth()->info()['account'], $pathInfo, $method)) {
//            return redirect('/backend/no_access');
//        }
        return $next($request);
    }

}
